(function($, im, doc) {
	var scroll = document.querySelector('#msg-list');
	var outerScroller = document.querySelector('.mui-content');

	//返回角度
	function GetSlideAngle(dx, dy) {
		return Math.atan2(dy, dx) * 180 / Math.PI;
	}

	//根据起点和终点返回方向 1：向上，2：向下，3：向左，4：向右,0：未滑动
	function GetSlideDirection(startX, startY, endX, endY) {
		var dy = startY - endY;
		var dx = endX - startX;
		var result = 0;

		//如果滑动距离太短
		if(Math.abs(dx) < 2 && Math.abs(dy) < 2) {
			return result;
		}

		var angle = GetSlideAngle(dx, dy);
		if(angle >= -45 && angle < 45) {
			result = 4;
		} else if(angle >= 45 && angle < 135) {
			result = 1;
		} else if(angle >= -135 && angle < -45) {
			result = 2;
		} else if((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
			result = 3;
		}
		return result;
	}
	var dragpage = function() {
		im.currentpage++;
		if(im.currentpage > im.pagenum) {
			im.currentpage = im.pagenum;
			outerScroller.removeEventListener('touchstart', touchstartfunc, false);
			outerScroller.removeEventListener('touchmove', touchmovefunc, false);
			outerScroller.removeEventListener('touchend', touchendfunc, false);
			mui.toast('没有数据啦！');
			return false;
		}

		var data = im.getDatas();
		var renderdata = im.parseChatData(data);
		im.areaMsgList.innerHTML = template('msg-template', {
			"record": renderdata,
			"header": im.header
		}) + im.areaMsgList.innerHTML;
		
		return true;
	}

	var touchStart = 0;
	var touchDis = 0;
	var startX, startY;
	var touchstartfunc = function(event) {
		var touch = event.targetTouches[0];
		// 把元素放在手指所在的位置 
		touchStart = touch.pageY;
		startX = event.touches[0].pageX;
		startY = event.touches[0].pageY;
	};
	var touchmovefunc = function(event) {
		var touch = event.targetTouches[0];
		//console.log(touch.pageY + 'px');
		scroll.style.top = scroll.offsetTop + touch.pageY - touchStart + 'px';
		//console.log(scroll.style.top);
		touchStart = touch.pageY;
		touchDis = touch.pageY - touchStart;
	};

	var touchendfunc = function(event) {
		touchStart = 0;
		var top = scroll.offsetTop;
		//console.log(scroll.offsetHeight);
		//console.log(scroll.style.top);
		//console.log(top);
		var endX, endY;
		endX = event.changedTouches[0].pageX;
		endY = event.changedTouches[0].pageY;
		var direction = GetSlideDirection(startX, startY, endX, endY);
		if(direction == 2) {
			if(top > 43 && im.pagenum > 1) {
				//dragpage();
			}
			/*
			if(top > 0) {
				var time = setInterval(function() {
					scroll.style.top = scroll.offsetTop - 2 + 'px';
					if(scroll.offsetTop <= 0) clearInterval(time);
				}, 1)
			}*/

		}

	};
	//outerScroller.addEventListener('touchstart', touchstartfunc, false);
	//outerScroller.addEventListener('touchmove', touchmovefunc, false);
	//outerScroller.addEventListener('touchend', touchendfunc, false);

}(mui, im, document));